package base.thread_demo.demo17;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 使用线程池启动定时器
 * ScheduledExecutorService 的使用
 */
public class ThreadPoolTimer {
    private static Logger log = LoggerFactory.getLogger(ThreadPoolTimer.class);

    public static void main(String[] args) {
        ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);

        /**
         * 创建定时执行任务
         */
        executor.schedule(new Runnable() {
            @Override
            public void run() {
                log.info("延迟 5s 爆炸");
                /**
                 * 今晚22点执行爆炸任务如何实现?
                 * 将5 改为 date.getTime() - System.currentTimeMillis(),
                 * TimeUnit.SECONDS 改为  TimeUnit.MILLISECONDS
                 */

            }
        }, 5, TimeUnit.SECONDS);


        /**
         * 创建固定频率的定时任务
         */
        executor.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                log.info("首次爆炸任务延迟5秒，往后每隔2s执行爆炸一次");
            }
        }, 5, 2, TimeUnit.SECONDS);

    }
}
